﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using FRL.DebtChased.Repository;

namespace FRL.DebtChased.ClockworkService
{

	/// <summary>
	/// Declaration of Messaging class
	/// </summary>
	public static class Messaging
	{

		/////////////////////////////////////////////////////////////
		// Static operations

		public static string SendSMS(string strMobileNo, string strBody)
		{

			// Declare return variable
			string strError = null;

			// No SMS function
			strError = "No SMS server available.";

			// Return variable
			return strError;

		}

		public static string SendEmail( string strAddress, string strSubject, string strMessage )
		{

			// Declare return variable
			string strError = null;

			try
			{

				// Create the mail message
				MailMessage msg = new MailMessage();
				msg.To.Add(new MailAddress(strAddress));
				msg.Subject = strSubject;
				msg.Body = strMessage;
				msg.From = new MailAddress("firstdefence@computerc.co.uk");
				msg.IsBodyHtml = true;
				msg.SubjectEncoding = Encoding.ASCII;

				// Create smtp client for sending
				SmtpClient smtpClient = new SmtpClient();
				smtpClient.Host = "barney.computercare.local";
				System.Net.NetworkCredential cred = new System.Net.NetworkCredential("richard.computerc.co.uk", "P4ssw0rd2010");
				smtpClient.Credentials = cred;
				smtpClient.UseDefaultCredentials = false;

				// Send message
				smtpClient.Send(msg);

			}
			catch (Exception x)
			{

				// Update error
				strError = string.Format("Failed to send email message.  {0}", Definitions.SeeErrorLog);

				// Log the error
				using (DebtChasedEntities ent = new DebtChasedEntities())
				{
					ErrorLog.Add(ent, "Sending email message.", x, null);
				}

			}

			// Declare return variable
			return strError;

		}

	}

}
